<template>
  <div class="container">
    <a-space direction="vertical" style="width: 100%" :size="16">
      <a-card style="width: 100%" title="已发卡券">
        <a-form :model="formState">
          <a-row :gutter="24">
            <a-col :span="6">
              <a-form-item label="卡券名称">
                <a-input
                  style="width: 100%"
                  v-model:value="formState.name"
                  placeholder="输入活动名称"
                />
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="卡券编号">
                <a-input
                  style="width: 100%"
                  v-model:value="formState.couponCode"
                  placeholder="输入卡券编号"
                />
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="用户账号">
                <a-input
                  style="width: 100%"
                  v-model:value="formState.username"
                  placeholder="输入卡券编号"
                />
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="卡券类别">
                <a-select
                  ref="select"
                  v-model:value="formState.couponCategory"
                  style="width: 100%"
                  placeholder="卡券类别"
                >
                  <a-select-option :value="0">全部</a-select-option>
                  <a-select-option :value="1">购物券</a-select-option>
                  <a-select-option :value="2">鉴定券</a-select-option>
                  <a-select-option :value="3">养护券</a-select-option>
                  <a-select-option :value="4">找货券</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="卡券来源">
                <a-select
                  ref="select"
                  v-model:value="formState.source"
                  style="width: 100%"
                  placeholder="活动角色"
                >
                  <a-select-option :value="0">全部</a-select-option>
                  <a-select-option :value="1">普通</a-select-option>
                  <a-select-option :value="2">会员</a-select-option>
                  <a-select-option :value="3">成长值</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="发放方式">
                <a-select
                  ref="select"
                  v-model:value="formState.releaseMode"
                  style="width: 100%"
                  placeholder="发放方式"
                >
                  <a-select-option :value="0">全部</a-select-option>
                  <a-select-option :value="1">自动发放</a-select-option>
                  <a-select-option :value="2">手动发放</a-select-option>
                  <a-select-option :value="3">活动领取</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="卡券状态">
                <a-select
                  ref="select"
                  v-model:value="formState.status"
                  style="width: 100%"
                  placeholder="卡券状态"
                >
                  <a-select-option :value="0">全部</a-select-option>
                  <a-select-option :value="1">未使用</a-select-option>
                  <a-select-option :value="2">已使用</a-select-option>
                  <a-select-option :value="3">已失效</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="领取时间">
                <a-range-picker
                  v-model:value="dateTime"
                  @change="handleChangeDate"
                />
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>

        <div class="layout-flexSpace">
          <a-space wrap>
            <a-button type="primary" @click="handleSearch">查询</a-button>
            <a-button @click="handleReset">重置</a-button>
          </a-space>
          <a-space wrap>
            <a-button type="primary" @click="handleExport">导出</a-button>
          </a-space>
        </div>
      </a-card>

      <a-card>
        <a-table
          :dataSource="dataSource"
          :columns="issuedColumns"
          :pagination="pagination"
          @change="handleChangeSize"
        >
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'couponCategory'">
              {{ couponCategoryNames[record.couponCategory - 1] }}
            </template>
            <template v-if="column.key === 'source'">
              {{ record.source == 1 ?'普通': record.source == 2? '会员':'成长值' }}
            </template>
            <template v-if="column.key === 'releaseMode'">
              {{ record.releaseMode == 1 ?'自动发放': record.releaseMode == 2? '手动发放':'活动领取' }}
            </template>
            <template v-if="column.key === 'status'">
              {{ record.status == 1 ?'未使用': record.status == 2? '已使用':'已失效' }}
            </template>
            <template v-if="column.key === 'action'">
              <a-space>
                <a-button @click="handleEdit(record)">查看</a-button>
              </a-space>
            </template>
          </template>
        </a-table>
      </a-card>
    </a-space>
    <cardIssuedModal ref="cardIssuedModalRef" />
  </div>
</template>
<script setup>
import { ref, onMounted } from "vue";
import { issuedColumns } from "./columns.js";
import { listUserCoupon, listMarket,exportUsercouponReceived } from "@/api/market";
import { exporeFile } from "@/utils/exporeFile";
import cardIssuedModal from './components/cardIssuedModal.vue'
const couponCategoryNames = ref(["购物券", "鉴定券", "养护券", "找货券"]);
const pagination = ref({
  current: 1,
  pageSize: 10,
  defaultPageSize: 10,
  total: 0,
  showSizeChanger: true,
  showTotal: (total) => `共 ${total} 条`,
});
const formState = ref({
  page: pagination.value.current,
  pageSize: pagination.value.pageSize,
  username: undefined,
  activityId: undefined,
  couponCategory: undefined,
  couponCode: undefined,
  startTime: undefined,
  endTime: undefined,
  startTime: undefined,
  releaseMode: undefined,
  status: undefined,
});
const dataSource = ref([]);
const getListUserCouponFn = async () => {
  const res = await listUserCoupon(formState.value);
  if (res.code === 200) {
    dataSource.value = res.data.list;
    pagination.value.total = res.data.total;
  }
};

function handleSearch() {
  formState.value.page = 1;
  formState.value.pageSize = 10;
  getListUserCouponFn();
}
// 获取活动
const activeList = ref([]);
const getistMarket = async () => {
  const res = await listMarket({ page: 1, pageSize: 999 });
  if (res.code === 200) {
    activeList.value = res.data.list;
  }
};
const dateTime = ref([null, null]);
function handleChangeDate(date, dateString) {
  dateTime.value = date;
  formState.value.startTime = dateString[0];
  formState.value.endTime = dateString[1];
}
function handleReset() {
  dateTime.value = [null, null];
  formState.value = {
    page: 1,
    pageSize: 10,
    username: undefined,
    activityId: undefined,
    couponCategory: undefined,
    couponCode: undefined,
    startTime: undefined,
    endTime: undefined,
    startTime: undefined,
    releaseMode: undefined,
    status: undefined,
  };
  pagination.value.current = 1
  pagination.value.pageSize = 10
  getListUserCouponFn();
}

const handleExport = async () => {
  const data = await exportUsercouponReceived(formState.value);
  exporeFile("已发卡券列表.xls", data);
};
function handleChangeSize(page) {
  formState.value.page = page.current;
  formState.value.pageSize = page.pageSize;
  pagination.value.current = page.current;
  pagination.value.pageSize = page.pageSize;
}
const cardIssuedModalRef = ref(null);
function handleEdit(record){
  cardIssuedModalRef.value.open(record.id)
}
onMounted(() => {
  getistMarket();
  getListUserCouponFn();
});
</script>

<style lang="less">
.img-item {
  display: flex;
  flex-direction: column;
  align-items: center;

  .img-label {
    margin-top: 6px;
    color: #9599a5;
    font-size: 12px;
  }
}
</style>
